// ! WordPress Mobi Reader  by  regou.me//
var wpmr={
    i:0,
    n:0,
    loaded:false,
    guid:null
}

wpmr.initInterface=function(){
    $("#wpmr_back>span").text(WpMobiReader_backbutton);
    $("#wpmr_comm > span").text(WpMobiReader_commentbutton);
}

wpmr.bindEvents=function(){
    $(".fullview_click").live("click",function(){  //点了看全文		
	var viewnum=$(this).data("viewnum");
	wpmr.fillFullview(viewnum);
	//修改HASH
	$(window).hashchange(function(){                
	    if(wpmr.guid && location.hash=="#wpmr_fullview"){
		location.hash=wpmr.guid;
	    }
	})
    });

    $("#wpmr_back").click(function(){
	$("#wpmr_fullview").children("section").empty();
    })
    var nextpageBtn=$("#wpmr_nextpage");
    nextpageBtn.click(function(){   //点了下一页
	if(wpmr.i<x.length){    
	    wpmr.n++;				
	    wpmr.writedown();    
	}
    })

    var prepageBtn=$("#wpmr_prepage");
    prepageBtn.click(function(){   //点了上一页
	if(wpmr.i>0 && WpMobiReader_itemsperpage*(wpmr.n-1)>=0){
	    wpmr.n--;
	    wpmr.i=WpMobiReader_itemsperpage*wpmr.n;				
	    wpmr.writedown();
	}
    })
	
    $("#wpmr_main").bind("swipeleft",function(){
	nextpageBtn.click();
    })
    $("#wpmr_main").bind("swiperight",function(){
	prepageBtn.click();
    })
	
}

//获取该文章的ID
wpmr.getGuid=function(link){
    var pos=link.indexOf('?p=');
    var id=link.substring(pos+3,link.length); 
    return id;
}

wpmr.del_bracket=function(str,b){ 
    var bracket=b || ']';
    var strback=str;
    var pos=str.indexOf(bracket);
    if(pos>=0){
	var new_str=str.substring(pos+bracket.length,str.length);  
	return new_str;
    }else{
	return strback
    }
}

wpmr.fillFullview=function(viewnum){//谱写详细页面的内容
    var wpmr_subtitle=wpmr.del_bracket(x[viewnum].getElementsByTagName("title")[0].childNodes[0].nodeValue);
    var wpmr_full=x[viewnum].getElementsByTagNameNS('*','encoded')[0].childNodes[0].nodeValue;
    wpmr_full='<h3>'+wpmr_subtitle+'</h3>'+wpmr_full;
    var wpmr_gocomments=x[viewnum].getElementsByTagName('comments')[0].childNodes[0].nodeValue;

    $("#wpmr_fullview > header > h1").text(wpmr_subtitle);
    $("#wpmr_fullview > section[data-role='content']").html(wpmr_full);
		
    $("#wpmr_comm").attr("href",wpmr_gocomments);
				
    var guidLink=x[viewnum].getElementsByTagName('guid')[0].childNodes[0].nodeValue;
    wpmr.guid=wpmr.getGuid(guidLink);
}

wpmr.writedown=function(){        
    $('body').addClass('ui-loading'); 
    var wp_string="";
    var wpmr_title,wpmr_pubDate,wpmr_comment_no,wpmr_creator;        
       
    for(;wpmr.i<x.length && wpmr.i<WpMobiReader_itemsperpage*(wpmr.n+1);wpmr.i++){
	try{
	    wpmr_title=x[wpmr.i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
	    wpmr_title=wpmr.del_bracket(wpmr_title);            
	    wpmr_pubDate=x[wpmr.i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue.substring(0,16);
	    wpmr_creator=x[wpmr.i].getElementsByTagNameNS('*','creator')[0].childNodes[0].nodeValue;
	    wpmr_comment_no=x[wpmr.i].getElementsByTagNameNS('http://purl.org/rss/1.0/modules/slash/','comments')[0].childNodes[0].nodeValue;                        
            
	    wp_string+='<div data-role="collapsible" ><h3>'+wpmr_title+'</h3><div><ul data-role="listview" data-mini="true"><li><a href="#wpmr_fullview" class="fullview_click" data-transition="slideup" data-viewnum="'+wpmr.i+'">'+WpMobiReader_fullview+'...</a></li><li>'+WpMobiReader_author+'：'+wpmr_creator+'</li><li>'+wpmr_comment_no+WpMobiReader_comnum+'</li><li>'+wpmr_pubDate+'</li></ul></div></div>';
            
	}catch(e){
	    alert(e)
	}  
    }    
    var ori_string='<div data-role="collapsible-set" data-iconpos="right" data-theme="'+WpMobiReader_theme+'" >';
    $("#wpmr_itemlist").html(ori_string+wp_string+'</div>').trigger('create');  
	
    $('body').removeClass('ui-loading'); 
}

wpmr.jump=function(){	
    var hash=wpmr.del_bracket(location.hash,'#');
    if(hash>1 && hash<=9999){
	//找到位置
	for(var i=0;i<=x.length;i++){
	    var guidLink=x[i].getElementsByTagName('guid')[0].childNodes[0].nodeValue;
	    var id=wpmr.getGuid(guidLink);
	    if(id==hash){
		wpmr.guid=id;			
		wpmr.fillFullview(i);
		$.mobile.changePage($("#wpmr_fullview"));			
		break;
	    }
	}
	
	
    }
}




$(document).bind('pagecreate',function(){      //All Start Here
    $("header").attr("data-theme",WpMobiReader_theme);
    if(!wpmr.loaded){        
	$.ajax({ 
	    url:WpMobiReader_rssurl, 
	    dataType: 'xml',
	    error: function(data){ 
		alert('Error loading XML document'+data); 
	    }, 
	    beforeSend: function(){  //issue :  Loading animate never hide
		//$.mobile.showPageLoadingMsg();
		$('body').addClass('ui-loading'); 
	    },
	    success: function(data){
		window.x=data.getElementsByTagName("item");
		wpmr.loaded=true;
		wpmr.jump();				
		wpmr.writedown();  				
		/*生成完成，开始绑定*/
		wpmr.bindEvents();
		wpmr.initInterface();				
	    },
	    complete: function(){
		//$.mobile.hidePageLoadingMsg();
		$('body').removeClass('ui-loading');
	    }
	});
    }

})